//
//  CoreDataManager.m
//  Lesson_5_记事本
//
//  Created by Alex on 16/5/31.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "CoreDataManager.h"

@interface CoreDataManager ()
@property (nonatomic,strong)NSManagedObjectContext *context;


@end
static CoreDataManager *coreData = nil;

@implementation CoreDataManager

- (NSManagedObjectContext *)context {
    if (!_context) {
        _context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
    }
    return _context;
}

+ (instancetype)shareCoreDataManager {
    if (!coreData) {
        coreData = [[CoreDataManager alloc]init];
    }
    return coreData;
}



- (instancetype)init
{
    self = [super init];
    if (self) {
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:@"Model" withExtension:@"momd"]];
        
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"read.sqlite"];
        NSLog(@"%@",filePath);
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:nil error:nil];
        self.context.persistentStoreCoordinator = store;
    }
    return self;
}

//查询整个收藏
- (NSArray *)selectAllCollectionModel {
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CollectionModel"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    
    
    return array;
}

//查看有没有已经存在的阅读收藏
- (BOOL)isContainsReadWithReadID:(NSNumber *)readID {
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CollectionModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"readID = %@",readID];
    request.predicate = predicate;
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    
    if (array.count != 0) {
        
        return YES;
    }else{
        
        return NO;
    }
}

//根据ReadID去删除一个收藏
- (void)deleteCollectionModelWithReadID:(NSNumber *)readID {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CollectionModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"readID = %@",readID];
    request.predicate = predicate;
   CollectionModel *collectionModel = [self.context executeFetchRequest:request error:nil].firstObject;
    [self.context deleteObject:collectionModel];
    [self.context save:nil];
}

// 插入
- (void)insertCollectionModelWithReadModel:(ReadListModel *)model html:(NSString *)html{
    
    if ([self isContainsReadWithReadID:model.ID]) {
        // 表示已经收藏
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经收藏了" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
    }
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
    CollectionModel *collectionModel = [NSEntityDescription insertNewObjectForEntityForName:@"CollectionModel" inManagedObjectContext:self.context];
    collectionModel.title = model.title;
    collectionModel.readID = [NSString stringWithFormat:@"%@",model.ID];
    collectionModel.coverimg = model.coverimg;
    collectionModel.name = model.name;
    collectionModel.html = html;
    [self.context save:nil];
    
}


@end
